package com.cloudclass.gateway.auth;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.cloudclass.api.user.constant.UserPermission;
import com.cloudclass.api.user.constant.UserRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class SaTokenConfigure {
    @Bean
    public StpInterface stpInterface(){
        return new StpInterfaceImpl();
    }
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 拦截地址
                .addInclude("/**")    /* 拦截全部path */
                // 开放地址
                .addExclude("/favicon.ico")
                // 鉴权方法：每次访问进入
                .setAuth(obj -> {
                    // 登录校验 -- 拦截所有路由，并排除/user/doLogin 用于开放登录
                    SaRouter.match("/**", "/user/login", r -> StpUtil.checkLogin());
//                    SaRouter.match("/**",r -> StpUtil.checkRole("adadad"));
                    // 权限认证 -- 不同模块, 校验不同权限
//                    SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
//                    SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
//                    SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
//                    SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));

                    // 更多匹配 ...  */
                })
                // 异常处理方法：每次setAuth函数出现异常时进入
                .setError(this::getSaResult);
    }
    private SaResult getSaResult(Throwable throwable){
        switch (throwable){
            case NotLoginException notLoginException:
                log.error("please login first");
                return SaResult.error("please login first");
            case NotRoleException exception:
                if (UserRole.ADMIN.name().equals(exception.getRole())) {
                    log.error("you don't have role to do this");
                    return SaResult.error("you don't have permission to do this");
                }
                log.error("you don't have permission to do this");
                return SaResult.error("you don't have permission to do this");
            case NotPermissionException exception:
                if (UserPermission.AUTH.name().equals(exception.getPermission())){
                    log.error("please auth first");
                    return SaResult.error("please auth first");
                }
                log.error("you don't have permission to do this");
                return SaResult.error("you don't have permission to do this");
            default:
                return SaResult.error(throwable.getMessage());
        }
    }


}
